package cn.kgc.javaweb.listner;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author: mosin
 * @version: v1.0  2022/6/24
 *
 * 监听session 对象中值的改变
 */
@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener {


    // 监听session中值的添加
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {

        System.out.println("session 中有值被添加");
        String name = event.getName();
        System.out.println("name = " + name);
        Object value = event.getValue();
        System.out.println("value = " + value);

    }

    // 监听session中值的移除
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {

        System.out.println("session 中有值被移除");
        String name = event.getName();
        System.out.println("name = " + name);
        Object value = event.getValue();
        System.out.println("value = " + value);
    }

    // 监听session中值的替换
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {

        System.out.println("session 中有值被替换");
        String name = event.getName();
        System.out.println("name = " + name);
        Object value = event.getValue();
        System.out.println("value = " + value);
    }
}
